﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


public class GamePanel : BasePanel<GamePanel>
{
    public Button btnFire;
    public Player player;

    public Toggle togSoundOn;
    public Toggle togSoundOff;
    public ToggleGroup togGroup;

    public Text txName;
    public Button btnChangeName;

    public Slider sliderSound;

    public Button btnBag;

    public Dropdown dropDown;
    public Light light;

    //血量值
    private float hp;

    /// <summary>
    /// 蓄能功能
    /// </summary>
    //关联长按功能按钮
    public LongPress longPress;
    //进度条跟对象 用于控制显隐
    public GameObject energyRoot;
    //进度条对象 用于控制对象
    public RectTransform energyProgress;
    //蓄能时间
    private float energyTime;
    //是否蓄能
    private bool energyState;
    //蓄能速度
    public float energySpeed;
    //蓄能增加的HP
    public float energyHp;

    /// <summary>
    /// 摇杆
    /// </summary>
    public RectTransform imgJoy;
    public EventTrigger joyEvent; 
    public float joyRadius;

    void Start()
    {
        btnFire.onClick.AddListener(()=>
        {
            player.Fire();
        });
        togSoundOn.onValueChanged.AddListener(TogChangeValue);
        togSoundOff.onValueChanged.AddListener(TogChangeValue);
        btnChangeName.onClick.AddListener(()=>
        {
            ChangeNamePanel.Instance.Show();
        });
        sliderSound.value = MusicData.volume;
        sliderSound.onValueChanged.AddListener((value)=> {
            MusicData.volume = value;
        });
        btnBag.onClick.AddListener(()=> {
            BagPanel.Instance.Show();
        });
        dropDown.onValueChanged.AddListener((index)=>{
            if (index == 0)
            {
                light.intensity = 1;
            }
            else
            {
                light.intensity = 0.3f;
            }
        });

        hp = 0;
        longPress.downEvene += EnergyDown;
        longPress.upEvene += EnergyUp;

        //摇杆注册事件
        //拖动中
        EventTrigger.Entry en = new EventTrigger.Entry();
        en.eventID = EventTriggerType.Drag;
        en.callback.AddListener(JoyDrag);
        joyEvent.triggers.Add(en);

        //拖动结束
        en = new EventTrigger.Entry();
        en.eventID = EventTriggerType.EndDrag;
        en.callback.AddListener(EndJoyDrag);
        joyEvent.triggers.Add(en);
    }

    private void JoyDrag(BaseEventData data)
    {
        PointerEventData eventData = data as PointerEventData;
        imgJoy.position += new Vector3(eventData.delta.x, eventData.delta.y, 0);

        //Vector2 nowPos;
        //RectTransformUtility.ScreenPointToLocalPointInRectangle(
        //    imgJoy.parent as RectTransform,
        //    eventData.position,
        //    eventData.enterEventCamera,
        //    out nowPos);
        //imgJoy.localPosition = nowPos; 

        //专门的参数,得到相对锚点的点
        if (imgJoy.anchoredPosition.magnitude > joyRadius)
        {
            //拉回来
            //单位向量 乘以 长度=临界位置
            imgJoy.anchoredPosition = imgJoy.anchoredPosition.normalized * joyRadius;
        }
        //让玩家移动
        player.Move(imgJoy.anchoredPosition);
    }

    private void EndJoyDrag(BaseEventData data)
    {
        imgJoy.anchoredPosition = Vector2.zero;
        player.Move(Vector2.zero);
    }

    private void TogChangeValue(bool v)
    {
        foreach (Toggle item in togGroup.ActiveToggles())
        {
            if (item == togSoundOn)
            {
                MusicData.soundOn = true;
            }
            else if (item == togSoundOff)
            {
                MusicData.soundOn = false;
            }
        }
    }

    public void SetNickName(string name)
    {
        this.txName.text = name;
    }

    public void EnergyDown()
    {
        energyState = true;
        energyProgress.sizeDelta = new Vector2(0, 40);
    }

    public void EnergyUp()
    {
        energyTime = 0;
        energyState = false;
        energyRoot.gameObject.SetActive(false);
    }

    public void ChangeHp(float value)
    {
        hp += value;
        Debug.LogFormat("ChangeHp value:{0} hp:{1}", value, hp);
    }

    private void Update()
    {
        if (energyState)
        {
            energyTime += Time.deltaTime;
            if (energyTime >= 0.2)
            {
                energyRoot.gameObject.SetActive(true);
                energyProgress.sizeDelta += new Vector2(energySpeed * Time.deltaTime, 0);
                if (energyProgress.sizeDelta.x >= 600)
                {
                    ChangeHp(energyHp);
                    energyProgress.sizeDelta = new Vector2(0, 40);
                }
            }
        }
    }
}
